<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>两数相加 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .code-dots {
            display: flex;
            gap: 8px;
        }
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            color: #e6e6e6;
            line-height: 1.6;
        }
        .keyword { color: #c678dd; }
        .function { color: #61afef; }
        .number { color: #d19a66; }
        .string { color: #98c379; }
        .comment { color: #5c6370; }
        .operator { color: #56b6c2; }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.1rem;
            color: #667eea;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">两数相加</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8">链表算法的优雅实现</p>
            <div class="flex justify-center gap-4 text-sm">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-link mr-2"></i>链表
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-calculator mr-2"></i>模拟
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-clock mr-2"></i>O(max(n,m))
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-puzzle-piece mr-3 text-purple-600"></i>
                    <span class="gradient-text">题目描述</span>
                </h2>
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap serif-font">给</span>定两个非空链表，表示两个非负整数。它们的每个节点存储一位数字，且是逆序存储（个位在链表头）。将这两个数相加，并以链表形式返回。
                </p>
                <div class="mt-6 bg-purple-50 rounded-xl p-6">
                    <h3 class="font-semibold text-purple-800 mb-3">
                        <i class="fas fa-lightbulb mr-2"></i>示例
                    </h3>
                    <div class="font-mono text-purple-700">
                        <p>输入: (2 → 4 → 3) + (5 → 6 → 4)</p>
                        <p>解释: 342 + 465 = 807</p>
                        <p>输出: 7 → 0 → 8</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visual Representation -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-project-diagram mr-3 text-purple-600"></i>
                    <span class="gradient-text">算法流程图</span>
                </h2>
                <div class="mermaid">
                    graph LR
                        A[开始] --> B[初始化哑节点]
                        B --> C{l1或l2或carry存在?}
                        C -->|是| D[获取当前位数值]
                        D --> E[计算总和与进位]
                        E --> F[创建新节点]
                        F --> G[移动指针]
                        G --> C
                        C -->|否| H[返回结果]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style H fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#f3f4f6,stroke:#667eea,stroke-width:2px
                        style C fill:#fef3c7,stroke:#f59e0b,stroke-width:2px
                        style D fill:#f3f4f6,stroke:#667eea,stroke-width:2px
                        style E fill:#f3f4f6,stroke:#667eea,stroke-width:2px
                        style F fill:#f3f4f6,stroke:#667eea,stroke-width:2px
                        style G fill:#f3f4f6,stroke:#667eea,stroke-width:2px
                </div>
            </div>
        </section>

        <!-- Core Concepts -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center">
                <span class="gradient-text">核心要点</span>
            </h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="text-4xl text-purple-600 mb-4">
                        <i class="fas fa-link"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">链表操作</h3>
                    <p class="text-gray-600">使用哑节点简化边界条件处理，避免对头节点的特殊判断</p>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="text-4xl text-purple-600 mb-4">
                        <i class="fas fa-plus-circle"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">进位处理</h3>
                    <p class="text-gray-600">通过整除和取余操作，优雅地处理进位逻辑</p>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="text-4xl text-purple-600 mb-4">
                        <i class="fas fa-sync-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">循环条件</h3>
                    <p class="text-gray-600">巧妙设计循环条件，同时处理不等长链表和最终进位</p>
                </div>
            </div>
        </section>

        <!-- Solution -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-lg overflow-hidden">
                <div class="bg-gradient-to-r from-purple-600 to-purple-800 text-white p-8">
                    <h2 class="text-3xl font-bold flex items-center">
                        <i class="fas fa-code mr-3"></i>
                        解决方案
                    </h2>
                </div>
                <div class="p-8">
                    <div class="mb-6">
                        <h3 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-brain mr-2 text-purple-600"></i>
                            解题思路
                        </h3>
                        <ul class="space-y-2 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                                <span>模拟加法过程，逐位相加，处理进位</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                                <span>使用哑节点（dummy node）简化链表操作</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                                <span>时间复杂度：O(max(n, m))，空间复杂度：O(max(n, m))</span>
                            </li>
                        </ul>
                    </div>

                    <div class="code-block">
                        <div class="code-header">
                            <div class="code-dots">
                                <div class="dot dot-red"></div>
                                <div class="dot dot-yellow"></div>
                                <div class="dot dot-green"></div>
                            </div>
                            <span class="text-gray-400 text-sm">Python</span>
                        </div>
                        <pre><code><span class="keyword">def</span> <span class="function">addTwoNumbers</span>(l1, l2):
    dummy = ListNode(<span class="number">0</span>)
    curr = dummy
    carry = <span class="number">0</span>
    
    <span class="keyword">while</span> l1 <span class="keyword">or</span> l2 <span class="keyword">or</span> carry:
        <span class="comment"># 获取当前位的值，如果链表已结束则为0</span>
        x = l1.val <span class="keyword">if</span> l1 <span class="keyword">else</span> <span class="number">0</span>
        y = l2.val <span class="keyword">if</span> l2 <span class="keyword">else</span> <span class="number">0</span>
        
        <span class="comment"># 计算总和与进位</span>
        total = x <span class="operator">+</span> y <span class="operator">+</span> carry
        carry = total <span class="operator">//</span> <span class="number">10</span>
        
        <span class="comment"># 创建新节点并移动指针</span>
        curr.next = ListNode(total <span class="operator">%</span> <span class="number">10</span>)
        curr = curr.next
        
        <span class="comment"># 移动输入链表指针</span>
        l1 = l1.next <span class="keyword">if</span> l1 <span class="keyword">else</span> <span class="keyword">None</span>
        l2 = l2.next <span class="keyword">if</span> l2 <span class="keyword">else</span> <span class="keyword">None</span>
    
    <span class="keyword">return</span> dummy.next</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Key Insights -->
        <section class="mb-16">
            <div class="bg-gradient-to-br from-purple-50 to-pink-50 rounded-2xl p-8">
                <h2 class="text-3xl font-bold mb-6 text-center">
                    <i class="fas fa-star mr-3 text-yellow-500"></i>
                    <span class="gradient-text">关键洞察</span>
                </h2>
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-white rounded-xl p-6 shadow-md">
                        <h3 class="font-bold text-lg mb-3 text-purple-800">
                            <i class="fas fa-lightbulb mr-2"></i>为什么使用哑节点？
                        </h3>
                        <p class="text-gray-700">哑节点避免了对头节点的